package com.college.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;


import com.college.domain.Batch;
import com.college.domain.ChangePassword;
import com.college.domain.College;
import com.college.domain.Department;
import com.college.domain.Semester;
import com.college.domain.Student;
import com.college.domain.Users;
import com.college.dto.MapSemesterDTO;
import com.college.interfaces.IConfiguration;
import com.college.interfaces.IConfigurationDAO;
import com.college.interfaces.IUserDAO;
import com.college.util.Encryption;

@Repository
@Transactional
public class ConfigurationDAO implements IConfigurationDAO {

	@Autowired
	private SessionFactory sessionFactory;	
	
	@Autowired
	private IConfiguration configuration;	
	
	@Autowired
	private IUserDAO userDAO;
	
	public College getCollegeById(int id) {
		
		return (College) sessionFactory.getCurrentSession().get(College.class, id);
	}

	
	public List<College> getCollegeByName(String name) {
		
		Criteria criteria = sessionFactory.getCurrentSession().createCriteria(College.class);
		criteria.add(Restrictions.ilike("collegeName", "%"+name+"%"));
		return criteria.list();
	}

	
	public List<College> getCollegeList() {
		Criteria criteria = sessionFactory.getCurrentSession().createCriteria(College.class);
		return criteria.list();
	}


	
	public int saveCollege(College college) {
		
		return (Integer) sessionFactory.getCurrentSession().save(college);
	}


	
	public int updateCollege(College college) {
		
		return (Integer) sessionFactory.getCurrentSession().merge(college);
	}


	
	public void deleteCollegeById(int id) {
		College college = getCollegeById(id);
	 sessionFactory.getCurrentSession().delete(college);
		
	}
	
	@Override
	public List<String> getCollegeListForDropDown() {
		List<String> collegeDropDownList = new ArrayList<String>();
		List<College> colleges = getCollegeList();
		for(College college: colleges){
			collegeDropDownList.add(college.getCollegeName());
		}
		return collegeDropDownList;
	}


	@Override
	public void deleteDeptById(int deptId) {
		Department dept = getDeptById(deptId);
		 sessionFactory.getCurrentSession().delete(dept);
		
	}


	@Override
	public List<Department> getAllDept() {
		Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Department.class);
		return criteria.list();
	}


	@Override
	public Department getDeptById(int deptId) {
		return (Department) sessionFactory.getCurrentSession().get(Department.class, deptId);
	}


	@Override
	public List<Department> getDeptByName(String deptName) {
		Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Department.class);
		criteria.add(Restrictions.ilike("deptName", deptName));
		return criteria.list();
	}


	@Override
	public int saveDept(Department dept) {
		List<College> collegeId = getCollegeByName(dept.getCollegeName());
		for(College college: collegeId){
			dept.setCollegeId(college.getCollegeId());		
		}
		Integer res =  (Integer)sessionFactory.getCurrentSession().save(dept);
		return res;
	}


	@Override
	public int updateDept(Department dept) {
		return (Integer) sessionFactory.getCurrentSession().merge(dept);
	}


	@Override
	public List<String> getDeptListForDropDown() {
		List<String> deptDropDownList = new ArrayList<String>();
		List<Department> dept = getAllDept();
		for(Department department: dept){
			deptDropDownList.add(department.getDeptName());
		}
		return deptDropDownList;
	}


	@Override
	public int saveBatch(Batch batch) {
		List<Department> deptList = getDeptByName(batch.getDeptName());
		for(Department depart: deptList){
			batch.setDeptId(depart.getDeptId())	;
		}
		Integer res =  (Integer)sessionFactory.getCurrentSession().save(batch);
		return res;
	}


	@Override
	public List<String> getAllBatchForDropDrown() {
		List<String> batchDropDownList = new ArrayList<String>();
		List<Batch> batch = getAllbatches();
		for(Batch batches: batch){
			batchDropDownList.add(batches.getBatchName());
		}
		return batchDropDownList;
	}


	@Override
	public List<Batch> getAllbatches() {
		Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Batch.class);
		return criteria.list();
	}


	@Override
	public List<Batch> getBatchByBatchName(String batchName) {
		Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Batch.class);
		criteria.add(Restrictions.ilike("batchName", batchName));
		return criteria.list();
	}


	@Override
	public int updateUserPassword(ChangePassword password, String userName) {
		int IReturnValue = 0;
		List<Users> userList = userDAO.getUserByUsername(userName);
		for(Users user:userList){
			if(user.getPassword().equals(Encryption.encrypt(password.getCurrentPassword())));
			{
				IReturnValue=2;
		boolean ispasswordsIdentical = configuration.isPasswordsIdentical(password);
		if(ispasswordsIdentical){
			password.setPassword(Encryption.encrypt(password.getPassword()));
			IReturnValue=1;
			try{
			sessionFactory.getCurrentSession().createQuery("update Users set password=:password where userName=:userName").setString("password", password.getPassword()).setString("userName", userName).executeUpdate();
			}
			catch (HibernateException He) {
				throw He;
			}
		}
		}
		}
		return IReturnValue;
	}


	@Override
	public List<MapSemesterDTO> getMappedBatchSem() {
		List<MapSemesterDTO> mapSemDTOList = new ArrayList<MapSemesterDTO>();
		List<Batch> batchList = getAllbatches();
		List<Semester> semRecords = getAllSemesters();
		for(Batch batch:batchList){
			MapSemesterDTO mapSemDTO = new MapSemesterDTO();
			mapSemDTO.setBatchName(batch.getBatchName());
			mapSemDTO.setDeptName(batch.getDeptName());
			for(Semester semrec:semRecords){
				if(semrec.getBatch().equals(batch.getBatchId())){
					mapSemDTO.setSemester(semrec.getSemesterName());
					mapSemDTO.setStatus(semrec.getStatus());
				}
				else{
					mapSemDTO.setSemester(null);
					mapSemDTO.setStatus(null);
				}
				
			}
			mapSemDTOList.add(mapSemDTO);
		}
		return mapSemDTOList;
	}


	@Override
	public List<Semester> getAllSemesters() {
		Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Semester.class);
		return criteria.list();
	}

	
	
	

}
